package hongcaosp.app.android.contact.friends;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.contact.PhoneUtil;
import hongcaosp.app.android.modle.bean.IUserDetail;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.StatusBarUtil;

public class LocalFindFriendActivity extends BaseActivity implements IListView<IUserDetail> {

    private RecyclerView recyclerView;
    private BaseAdapter<IUserDetail> adapter;
    private LocalFindFriendPresenter localFindFriendPresenter;

    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this, false);
        recyclerView = findViewById(R.id.recycler_view);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        adapter = new BaseAdapter<IUserDetail>() {
            @NonNull
            @Override
            public BaseHolder<IUserDetail> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                FriendHolder friendHolder = new FriendHolder(LayoutInflater.from(LocalFindFriendActivity.this).inflate(R.layout.item_friend,parent,false), LocalFindFriendActivity.this);
                friendHolder.setShowFans(false);
                return friendHolder;
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        localFindFriendPresenter = new LocalFindFriendPresenter(this);

        AndPermission.with(this).runtime().permission(Manifest.permission.READ_CONTACTS).onDenied(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                ToastManager.getInstance().showToast("无法获取通讯录好友");
            }
        }).onGranted(new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                PhoneUtil phoneUtil = new PhoneUtil(LocalFindFriendActivity.this);
                localFindFriendPresenter.fresh(phoneUtil.getPhone());
            }
        }).start();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_localfriend;
    }

    @Override
    public void freshList(List<IUserDetail> data, boolean canLoadMore) {
        adapter.setData(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void addList(List<IUserDetail> data, boolean canLoadMore) {

    }

    public static void toActivity(Context context) {
        if (context != null) {
            Intent intent = new Intent(context,LocalFindFriendActivity.class);
            context.startActivity(intent);
        }
    }
}
